package com.cloudcc.frame.timejob;

import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.trigger.TriggerEditor;
import com.cloudcc.frame.Utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HHQGJOB  extends TriggerEditor {
    /*
     * @Description:定时丢单
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void lost_eleopp(){
        String GJZ="跟进中";
        String YDD="已丢单";
        String jieduan5="5-丢单";
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String today = sf.format(date);
        String now=sf2.format(date);
        DateUtil dateUtil = new DateUtil();
        double line=-1;
        String reason="到期自动丢单";
        //查询商机sql
        String eleopp_sql="select id,khmc,createdate,name,fk_psmc,jyzx,ownerid from eleopp where is_deleted='0' and nd status1='"+GJZ+"'";
        //查询管户小组sql

        //丢单sql
        String update_sql="update eleopp set jieduan='"+jieduan5+"',status1='"+YDD+"',lose_time='"+now+"',sqyy='"+reason+"' where id=";
        List<CCObject> eleopp_list = this.cqlQuery("eleopp", eleopp_sql);
        if(eleopp_list.size()>0){
            for (CCObject cc:
                    eleopp_list) {
                //判断是否大于90
                String createdate=cc.get("createdate")==null?"":cc.get("createdate")+"";
                Long aLong = dateUtil.between_days(createdate, today);
                if(aLong>line){
                    String ele_id=cc.get("id")==null?"":cc.get("id")+"";
                    String fk_psmc=cc.get("fk_psmc")==null?"":cc.get("fk_psmc")+"";
                    String name=cc.get("name")==null?"":cc.get("name")+"";
                    String ownerid=cc.get("ownerid")==null?"":cc.get("ownerid")+"";
                    String sql=update_sql+"'"+ele_id+"'";
                    String khmc=cc.get("khmc")==null?"":cc.get("khmc")+"";

                    this.cqlQuery("eleopp",sql);
                    String manager_sql="select * from manage_group where fk_psmc='"+fk_psmc+"' and fk_account='"+khmc+"'";
                    List<CCObject> manage_group = this.cqlQuery("manage_group", manager_sql);
                    if(manage_group.size()>0){
                        manage_group.get(0).put("ghzt",YDD);
                        this.update(manage_group.get(0));
                    }
                    CCObject notice = new CCObject("notice");
                    notice.put("accountId",ownerid);
                    notice.put("companyName",name);
                    notice.put("noticeInfo","你的商机为："+name+"已丢单");
                    this.insert(notice);
                }

            }
        }

    }
    /*
     * @Description:定时提醒
     * @T(n):
     * @Author: zxb
     * @Date:
     * @param null:
     * @return: null
     **/
    public void remain_eleopp(){
        String GJZ="跟进中";
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sf.format(date);
         DateUtil dateUtil = new DateUtil();
        double line=76.0;
        //查询商机sql
        String eleopp_sql="select id,khmc,createdate,name,fk_psmc,ownerid from eleopp where is_deleted='0' and status1='"+GJZ+"'";

        List<CCObject> eleopp_list = this.cqlQuery("eleopp", eleopp_sql);
        if(eleopp_list.size()>0){
            for (CCObject cc:
                    eleopp_list) {
                //判断是否大于90
                String createdate=cc.get("createdate")==null?"":cc.get("createdate")+"";
                Long aLong = dateUtil.between_days(createdate, today);
                if(aLong>line){
                    String name=cc.get("name")==null?"":cc.get("name")+"";
                    String khmc=cc.get("khmc")==null?"":cc.get("khmc")+"";
                    String ownerid=cc.get("ownerid")==null?"":cc.get("ownerid")+"";
                    CCObject notice = new CCObject("notice");
                    notice.put("accountId",ownerid);
                    notice.put("companyName",name);
                    notice.put("noticeInfo","你的商机为："+name+"创建之后76天未开函，还有14天自动丢单");
                    this.insert(notice);
                }

            }
        }

    }
}
